import { UserConfigExport, ConfigEnv, loadEnv } from 'vite'
import path from 'path'
import uni from '@dcloudio/vite-plugin-uni'
import Unocss from 'unocss/vite'
import { warpperEnv } from './build/utils'
import { createProxy } from './build/proxy'
const alias: Record<string, string> = {
  '@': path.resolve(__dirname, './src'),
  '@components': path.resolve(__dirname, './src/components')
}

const root: string = process.cwd()

export default ({ mode }: ConfigEnv): UserConfigExport => {
  const { VITE_BASE_PROXY, VITE_PROXY_DOMAIN_REAL ,VITE_APP_MATRIX} = warpperEnv(
    loadEnv(mode, root)
  )
const MATRIXSCSS = VITE_APP_MATRIX === 'DEV'?'@import "@/styles/vars/dev.scss";': VITE_APP_MATRIX === 'RECEIVE'?'@import "@/styles/vars/receive.scss";':VITE_APP_MATRIX === 'PRODUCT'?'@import "@/styles/vars/product.scss";':''
const additionalData = '@import "@/styles/vars/_base.scss";'+ MATRIXSCSS 
  return {
    plugins: [uni(), Unocss()],
    root,
    resolve: {
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
      alias
    },
    // 服务端渲染
    server: {
      host: '0.0.0.0',
      // 本地跨域代理
      proxy: createProxy(VITE_BASE_PROXY, VITE_PROXY_DOMAIN_REAL)
    },
    css: {
      // 配置`scss`和`less`全局变量
      preprocessorOptions: {
        scss: {
          additionalData: additionalData
        },
        less: {
          additionalData: '@import "@/styles/vars/_base.less";' 
        }
      },
    },
    define: {
      __INTLIFY_PROD_DEVTOOLS__: false
    }
  }
}

